import Vue from "vue";
import Router from "vue-router";
Vue.use(Router);

import Layout from "@/layout"; // 布局

/* 静态路由 */
import cRouters from "./constantRouter.js";
export const constantRoutes = cRouters;

/* 动态加载modules文件夹下的router */
/* 动态加载无法排序一级菜单 */
// const modulesFiles = require.context("./modules", true, /\.js$/);
// const modules = modulesFiles.keys().reduce((modules, modulePath) => {
//   const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
//   const value = modulesFiles(modulePath);
//   modules[moduleName] = value.default;
//   return modules;
// }, {});
// const modulesRouter = [];
// for (let key in modules) {
//   modulesRouter.push(modules[key]);
// }

import userRouters from "./modules/user.js";
import businessRouter from "./modules/business.js"; // 应用中心
import applicationRouter from "./modules/application.js"; // 应用管理
import accountRouter from "./modules/account.js"; // 账号管理
import dictionaryRouter from "./modules/dictionary.js";
export const asyncRoutes = [
  // ...modulesRouter,
  userRouters,
  ...businessRouter,
  applicationRouter,
  accountRouter,
  dictionaryRouter,

  // 404 页面需要最后被加载
  { path: "*", redirect: "/404", hidden: true }
];

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
});

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
